import axios from 'axios'

export default {
  namespaced: true,
  
  state: {
    reports: [],
    loading: false,
    error: null
  },
  
  getters: {
    allReports: state => state.reports,
    loading: state => state.loading,
    error: state => state.error
  },
  
  mutations: {
    SET_LOADING(state, status) {
      state.loading = status;
    },
    SET_ERROR(state, error) {
      state.error = error;
    },
    SET_REPORTS(state, reports) {
      state.reports = reports;
    },
    ADD_REPORT(state, report) {
      state.reports.unshift(report);
    },
    UPDATE_REPORT(state, updatedReport) {
      const index = state.reports.findIndex(report => report.id === updatedReport.id);
      if (index !== -1) {
        state.reports.splice(index, 1, updatedReport);
      }
    },
    DELETE_REPORT(state, id) {
      state.reports = state.reports.filter(report => report.id !== id);
    }
  },
  
  actions: {
    // 获取所有健康报告
    async fetchReports({ commit }) {
      try {
        commit('SET_LOADING', true);
        commit('SET_ERROR', null);
        
        const response = await axios.get('/health/reports');
        commit('SET_REPORTS', response.data);
        
        commit('SET_LOADING', false);
        return response.data;
      } catch (error) {
        commit('SET_LOADING', false);
        commit('SET_ERROR', error.response?.data?.message || '获取健康报告失败');
        throw error;
      }
    },
    
    // 上传健康报告
    async uploadReport({ commit }, formData) {
      try {
        commit('SET_LOADING', true);
        commit('SET_ERROR', null);
        
        const response = await axios.post('/health/reports', formData, {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        });
        
        commit('ADD_REPORT', response.data);
        commit('SET_LOADING', false);
        return response.data;
      } catch (error) {
        commit('SET_LOADING', false);
        commit('SET_ERROR', error.response?.data?.message || '上传健康报告失败');
        throw error;
      }
    },
    
    // 更新健康报告
    async updateReport({ commit }, { id, reportData }) {
      try {
        commit('SET_LOADING', true);
        commit('SET_ERROR', null);
        
        const response = await axios.put(`/health/reports/${id}`, reportData);
        commit('UPDATE_REPORT', response.data);
        
        commit('SET_LOADING', false);
        return response.data;
      } catch (error) {
        commit('SET_LOADING', false);
        commit('SET_ERROR', error.response?.data?.message || '更新健康报告失败');
        throw error;
      }
    },
    
    // 删除健康报告
    async deleteReport({ commit }, id) {
      try {
        commit('SET_LOADING', true);
        commit('SET_ERROR', null);
        
        await axios.delete(`/health/reports/${id}`);
        commit('DELETE_REPORT', id);
        
        commit('SET_LOADING', false);
        return true;
      } catch (error) {
        commit('SET_LOADING', false);
        commit('SET_ERROR', error.response?.data?.message || '删除健康报告失败');
        throw error;
      }
    },
    
    // 获取报告详情
    async fetchReportById({ commit }, id) {
      try {
        commit('SET_LOADING', true);
        commit('SET_ERROR', null);
        
        const response = await axios.get(`/health/reports/${id}`);
        
        commit('SET_LOADING', false);
        return response.data;
      } catch (error) {
        commit('SET_LOADING', false);
        commit('SET_ERROR', error.response?.data?.message || '获取健康报告详情失败');
        throw error;
      }
    }
  }
}